package com.itheima.accountingdb.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.accountingdb.mapper.TransactionMapper;
import com.itheima.accountingdb.pojo.PageBean;
import com.itheima.accountingdb.pojo.Transaction;
import com.itheima.accountingdb.service.TransactionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author panweiwei
 */
@Service
public class TransactionServiceImpl extends ServiceImpl<TransactionMapper, Transaction> implements TransactionService{

    @Autowired
    private TransactionMapper transactionMapper;

    /**
     * 分页查询交易记录
     *
     * @param page       页码
     * @param size       每页显示的条数
     * @param startDate  开始时间
     * @param endDate    结束时间
     * @param type       交易类型
     * @param categoryId 分类id
     * @return
     */
    @Override
    public PageBean<Transaction> list(Integer page, Integer size, String startDate, String endDate, String type, String categoryId,long isDeleted) {
        PageBean<Transaction> pageBean = new PageBean<>();
        //开始分页查询
        PageHelper.startPage(page, size);
        List<Transaction> ta = transactionMapper.list(page, size, startDate, endDate, type, categoryId,isDeleted);
        Page<Transaction> transactionPage = (Page<Transaction>) ta;
        pageBean.setTotal(transactionPage.getTotal());
        pageBean.setItems(transactionPage.getResult());
        return pageBean;
    }

    @Override
    public Transaction add(Transaction transaction) {
       int rows = transactionMapper.add(transaction);
       if (rows <= 0){
           return null;
       }
       return transaction;
    }

    /**
     * 修改交易记录
     *
     * @param transaction
     * @return
     */
    @Override
    public Transaction update(Transaction transaction,Integer id) {
        transaction.setId(Long.valueOf(id));
        int rows = transactionMapper.update(transaction);
        if (rows <=0){
            return null;
        }
        return transaction;
    }

    @Override
    public void delete(Long id) {
        transactionMapper.delete(id);
    }
}
